package com.supreme.aspectserver.service.simple;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class LockService {

    private static final Object staticObjectLock = new Object();
    private final Object objectLock = new Object();

    synchronized public void syncLockMethod(Integer index) {
        String threadName = Thread.currentThread().getName();
        log.debug("syncLockMethod threadName = {} start = {}", threadName, index);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.debug("syncLockMethod threadName = {} finish = {}", threadName, index);
    }

    synchronized public static void syncStaticLockMethod(Integer index) {
        String threadName = Thread.currentThread().getName();
        log.debug("syncStaticLockMethod threadName = {} start = {}", threadName, index);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.debug("syncStaticLockMethod threadName = {} finish = {}", threadName, index);
    }

    public void syncObjectLockMethod(Integer index) {
        synchronized (objectLock) {
            String threadName = Thread.currentThread().getName();
            log.debug("syncObjectLockMethod threadName = {} start = {}", threadName, index);
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.debug("syncObjectLockMethod threadName = {} finish = {}", threadName, index);
        }
    }

    public void syncStaticObjectLockMethod(Integer index) {
        synchronized (staticObjectLock) {
            String threadName = Thread.currentThread().getName();
            log.debug("syncStaticObjectLockMethod threadName = {} start = {}", threadName, index);
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.debug("syncStaticObjectLockMethod threadName = {} finish = {}", threadName, index);
        }
    }
}
